<?php
/**
 * Created by PhpStorm.
 * Date: 2018/3/7
 * Time: 11:28
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Models\Member;


class MemberController extends ApiController
{
    // 新增会员信息
    public function memberAdd(Request $request)
    {
        // 表单验证
        $validator = Validator::make($request->all(),
            [
                'name' => 'required|max:10',
                'mobile' => 'required|max:11',
                'birthday' => 'required',
                'gender' => 'required',
                'licence_plate' => 'required',
                'driving_time' => 'required',
                'role' =>'required'
            ],
            [
                'name.required' => '姓名参数缺失',
                'mobile.required' => '电话参数缺失',
                'birthday.required' => '生日参数缺失',
                'gender.required' => '性别参数缺失',
                'licence_plate.required' => '车牌号参数缺失',
                'driving_time.required' => '行驶证日期缺失',
                'role.required' =>'会员类型缺失'
                
            ]
            );
        if ($validator->fails()) {
            return $this->failed($validator->errors(), 401);
        }
        // 这里后面加上valicate验证
        if ($request->input('id')) {
            // 编辑
            $Member = Member::find($request->input('id'));
        } else {
            $Member = new Member();
        }
        
    
        $Member->id = 8;
        $Member->name = $request->input('name');
        $Member->birthday = $request->input('birthday');
        $Member->gender = $request->input('gender');
        $Member->licence_plate = $request->input('licence_plate');
        $Member->driving_time = $request->input('driving_time');
        $Member->role = $request->input('role');
        $Member->save();
        if ($Member->id) {
            return $this->message('操作成功');
        };
        return $this->failed('操作失败', 401);
    }
    

}